<%@page import="fr.uniscol.model.IUniscolUser"%>
<%@page import="fr.uniscol.model.DefaultUser"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt_rt" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

<jsp:useBean id="utilisateurs" type="java.util.List<fr.uniscol.model.DefaultUser>" scope="request"/>

    

<script type="text/javascript">
<!--
	$(document).ready(function(){
		$(".addUser").click(function(){
			editUser(0);
		});	
		
		$("#btnEnregistrer").click(saveUser);
	});
	
	function editUser(userId){
		$("div.modal-body").load("user", {"id":userId},  function(){
			$("#userFormModal").modal("show");
		});
	}
	
	function saveUser(){
		
		$.ajax({url:$("form[name='frmUser']").attr("action"),
			   type: "POST",
			   data: $("form[name='frmUser']").serialize(),
			   success : function(data){
				   if(data.code == 0){
					   alert(data.message);
				   	   location.reload();
				   	   
				   }else{
					   alert(data.message);
				   }
			   },
			   error : function(){
				   alert("ECHEC: Impossible de joindre le serveur !");
			   },
			   dataType:"json"}
		);
	}
//-->
</script>

<div class="container-fluid">

<div class="row">
	<div class="col-sm-offset-1 col-md-6">
		<h4>Utilisateurs</h4>
		<table class="table table-striped table-hover">
			<thead>
				<tr>
					<th>ID</th>
					<th>Login</th>
					<th>Nom Prénom</th>
					<th class="buttons"><button id="btnAddUser" type="button" class="btn btn-primary btn-sm addUser">Ajouter un utilisateur</button></th>
				</tr>
			</thead>
			<tbody>
<%
	for(IUniscolUser user : utilisateurs){
%>
				<tr>
					<td><%=user.getId() %></td>
					<td><%=user.getLogin() %></td>
					<td><%=user.getIdentite() %></td>
					<td class="buttons">
						<button type="button" class="btn btn-primary btn-xs" onClick="javascript:editUser(<%=user.getId() %>)">Modifier</button>
						<button type="button" class="btn btn-default btn-xs" onClick="javascript:location='user.delete?id=<%=user.getId() %>'">Supprimer</button>
					</td>
				</tr>
<%	} %>
			</tbody>
			<tfoot>
				<tr>
					<td colspan="5" class="buttons">
						<button id="btnAddUser" type="button" class="btn btn-primary btn-sm addUser">Ajouter un utilisateur</button>
					</td>
				</tr>
			</tfoot>
		</table>
	</div>
</div>  

<!-- MODAL -->
<div id="userFormModal" class="modal fade">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title">Nouvel utilisateur</h4>
      </div>
      <div class="modal-body">

      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Annuler</button>
        <button id="btnEnregistrer" type="submit" class="btn btn-primary">Enregistrer</button>
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

</div>

